<template>
  <el-dialog :close-on-click-modal="false" class="update-img" top="25vh" :visible="dialogShow" center width="420px" @close="btnClose">
    <div class="center">
      <el-form ref="form" :model="imgFormData" :rules="formRules">
        <el-form-item prop="name" label="重命名">
          <el-input v-model="imgFormData.name" size="mini" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="btnOk()">确定</el-button>
        <el-button size="small" @click="btnClose()">取消</el-button>
      </span>
    </div>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import { updateImgName } from '@/api/image'
export default {
  name: 'UpdateImg',
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      default: () => { }
    }
  },
  data() {
    return {
      imgFormData: {
        id: '',
        name: ''
      },
      formRules: {
        name: [{ required: true, message: '图片名不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['imageList'])
  },
  watch: {
    formData: {
      handler(val) {
        this.imgFormData.id = val.id
        this.imgFormData.name = val.name
      },
      immediate: true
    }
  },
  methods: {
    btnClose() {
      this.$emit('update:dialogShow', false)
    },
    async btnOk() {
      try {
        await updateImgName(this.imgFormData.id, this.imgFormData.name)
        this.$emit('updateImgName', this.imgFormData.name)
        this.$message.success('修改成功')
      } catch (error) {
        console.log(error)
        this.$message.error('修改失败')
      }
      this.btnClose()
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-dialog {
    height: unset;
    border-radius: 5px;

    .el-form {
        padding: 10px;
    }

    .el-input {
        padding-top: 10px;
    }

    .el-dialog__header {
        padding: 0;
    }

    .el-dialog__body {
        padding: 10px 15px;
    }
}
</style>

